<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/19
 * Time: 17:05
 */
namespace Admin\Model;

use Think\Model;

class FriendModel extends Model
{
    protected $trueTableName = 'friends';


    public function friendsAdd()
    {
        $name = I('post.title');
        $target = I('post.target');
        $sort = (int)I('post.sort');
        $image = I('post.image');

        if (empty($name)) {
            $this->error = '名称必须填写';
            return false;
        }
        if (mb_strlen($name) > 50) {
            $this->error = '名称长度不能超过50个字';
            return false;
        }
        if (empty($target)) {
            $this->error = '链接地址必须填写';
            return false;
        }
        if (mb_strlen($target) > 255) {
            $this->error = '链接长度不能超过255个字';
            return false;
        }
        if (empty($sort)) {
            $this->error = '排序不能为空';
            return false;
        }
        if (!is_integer($sort)) {
            $this->error = '排序必须为整数';
            return false;
        }
        if (empty($image)) {
            $this->error = '必须上传图片';
            return false;
        }

        $this->friend_name = $name;
        $this->target = $target;
        $this->sort = $sort;
        $this->image = $image;

        if ($this->add()) {
            return true;
        } else {
            return false;
        }
    }

    public function friendsModify()
    {
        $id = I('post.id');
        $name = I('post.title');
        $target = I('post.target');
        $sort = (int)I('post.sort');
        $image = I('post.image');

        if (empty($name)) {
            $this->error = '名称必须填写';
            return false;
        }
        if (mb_strlen($name) > 50) {
            $this->error = '名称长度不能超过50个字';
            return false;
        }
        if (empty($target)) {
            $this->error = '链接地址必须填写';
            return false;
        }
        if (mb_strlen($target) > 255) {
            $this->error = '链接长度不能超过255个字';
            return false;
        }
        if (empty($sort)) {
            $this->error = '排序不能为空';
            return false;
        }
        if (!is_integer($sort)) {
            $this->error = '排序必须为整数';
            return false;
        }
        if (empty($image)) {
            $this->error = '必须上传图片';
            return false;
        }

        $this->friend_name = $name;
        $this->target = $target;
        $this->sort = $sort;
        $this->image = $image;

        if ($this->where(array('id'=>array('eq',$id)))->save()) {
            return true;
        } else {
            return false;
        }
    }

}